﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNetUniLodz.Models;

namespace DotNetUniLodz.Controllers
{
    public class EventsController : Controller
    {
        private DotNetULEntities db = new DotNetULEntities();

        //
        // GET: /Events/

        public ActionResult Index()
        {
            var @events = db.Events.Include(e => e.User);
            return View(@events.ToList().Reverse<Event>());
        }

        //
        // GET: /Events/Details/5

        public ActionResult Details(int id = 0)
        {
            Event @event = db.Events.Find(id);
            if (@event == null)
            {
                return HttpNotFound();
            }
            return View(@event);
        }

        //
        // GET: /Events/Create

        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Events/Create

        [Authorize(Roles = "Admin")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)] //to ma pozwolić na "Łykanie" htmla jako text
        public ActionResult Create(Event @event)
        {
            if (ModelState.IsValid)
            {
                @event.User_author = UserModel.GetCurrentUser(User.Identity.Name).User_id;
                db.Events.Add(@event);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(@event);
        }

        //
        // GET: /Events/Edit/5

        [Authorize(Roles = "Admin")]
        public ActionResult Edit(int id = 0)
        {
            Event @event = db.Events.Find(id);
            if (@event == null)
            {
                return HttpNotFound();
            }
            ViewBag.User_author = new SelectList(db.Users, "User_id", "User_login", @event.User_author);
            return View(@event);
        }

        //
        // POST: /Events/Edit/5

        [Authorize(Roles = "Admin")]
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)] //to ma pozwolić na "Łykanie" htmla jako text
        public ActionResult Edit(Event @event)
        {
            if (ModelState.IsValid)
            {
                db.Entry(@event).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.User_author = new SelectList(db.Users, "User_id", "User_login", @event.User_author);
            return View(@event);
        }

        //
        // GET: /Events/Delete/5

        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id = 0)
        {
            Event @event = db.Events.Find(id);
            if (@event == null)
            {
                return HttpNotFound();
            }
            return View(@event);
        }

        //
        // POST: /Events/Delete/5

        [Authorize(Roles = "Admin")]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Event @event = db.Events.Find(id);
            foreach (var article in db.Articles.Where(a => a.Event_id == @event.Event_id))
            {
                article.Event = null;
                article.Event_id = null;
            }
            db.Events.Remove(@event);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}